package cn.keniykey.mc.landterritory.command.only;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cn.keniykey.mc.landterritory.command.CommandUtil;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.landview.LandMainView;
import cn.keniykey.mc.landterritory.landview.LandQueryView;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewIntent;

public final class CommandLands implements CommandExecutor{

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String arg, String[] args) {
		if(!(sender instanceof Player)) {
			CommandUtil.sendMustPlayer(sender);
			return true;
		}
		final Player player = (Player) sender;
		OfflinePlayer offplayer_tager = null;
		if(args.length > 0) {
			final Player player_tager = Bukkit.getPlayer(args[0]);
			if(player_tager == null) {
				for(final OfflinePlayer offplayer : Bukkit.getOfflinePlayers()) {
					if(offplayer.getName().equals(args[0])) {
						offplayer_tager = offplayer;
						break;
					}
				}
			}else {
				offplayer_tager = player_tager;
			}
		}else {
			offplayer_tager = player;
		}
		if(offplayer_tager == null) {
			player.sendMessage(LanguageManager.getChatLang(1202100, args[0]));
			return true;
		}
		player.closeInventory();
		if(player.getUniqueId().equals(offplayer_tager.getUniqueId())) {
			AbFrameView.showView(player, new FrameViewIntent(LandMainView.class));
		}else {
			final FrameViewBundle bundle = new FrameViewBundle();
			final FrameViewIntent intent = new FrameViewIntent(LandQueryView.class);
			bundle.putValue("player", offplayer_tager);
			intent.putExtras(bundle);
			AbFrameView.showView(player, intent);
		}
		return true;
	}

}